//
//  updateView.m
//  phmall
//
//  Created by admin on 2017/9/14.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "UpdateView.h"
#import "GoodsListCell.h"
#import "CategoryModel.h"
#import "GoodsModel.h"
@interface UpdateView ()<UICollectionViewDataSource,UICollectionViewDelegate,GoodsListCellDelegate,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>

@property(nonatomic,strong)UICollectionView *collectionView;
@end

@implementation UpdateView
-(void)moreAction:(UICollectionViewCell *)cell{
    for (int i=0; i<self.timeArr.count; i++) {
        NSMutableArray *goodsArr=self.timeArr[i];
        for (int j=0; j<goodsArr.count; j++) {
            NSIndexPath *index1 =  [NSIndexPath indexPathForItem:j inSection:i];
            GoodsListCell *goodsCell=(GoodsListCell *)[self.collectionView cellForItemAtIndexPath:index1];
            if (cell!=goodsCell) {
                [goodsCell moreViewTapAction];
            }
        }
    }
}
-(instancetype)init{
    if (self=[super init]) {
        self.emptyType=EmptyDataSetNone;
        [self addSubview:self.collectionView];
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    CGSize superSize = self.frame.size;
    
    if (CGSizeEqualToSize(superSize, CGSizeZero)) {
        return;
    }
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self);
    }];
}
- (void)analysisDataWithNewArray:(NSArray*)array{
    self.emptyType=EmptyDataSetNoData;
    NSMutableArray *goods=[[NSMutableArray alloc] init];
    for (NSDictionary *dict in array)
    {
        GoodsModel *model = [GoodsModel yy_modelWithDictionary:dict];
        [goods addObject:model];
    }
    self.timeArr=[self timeSort:goods];;
    [self.collectionView reloadData];
}
-(NSMutableArray *)timeSort:(NSMutableArray *)array2{
    NSMutableArray *array = [NSMutableArray arrayWithArray:array2];
    NSMutableArray *dateMutablearray = [@[] mutableCopy];
    for (int i = 0; i < array.count; i ++) {
        GoodsModel *model=array[i];
        NSString *string = model.register_date;
        NSMutableArray *tempArray = [@[] mutableCopy];
        [tempArray addObject:model];
        for (int j = i+1; j < array.count; j ++) {
            GoodsModel *jmodel=array[j];
            NSString *jstring = jmodel.register_date;
            
            if([string isEqualToString:jstring]){
                
                [tempArray addObject:jmodel];
                
                [array removeObjectAtIndex:j];
                j -= 1;
                
            }
        }
        [dateMutablearray addObject:tempArray];
    }
    return dateMutablearray;
}
#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.timeArr.count;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSMutableArray *goodsArr=self.timeArr[section];
    return goodsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId=@"cell";
    GoodsListCell *cell = (GoodsListCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    NSMutableArray *goodsArr=self.timeArr[indexPath.section];
    cell.delegate=self;
    cell.model=goodsArr[indexPath.row];
    ViewBorderRadius(cell, 10, 0, [UIColor clearColor]);
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(SCREEN_WIDTH, 44);
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (iPhone5SE) {
        return CGSizeMake(SCREEN_WIDTH/2-15, 230);
    }else  if (iPhone6_6s) {
        return CGSizeMake(SCREEN_WIDTH/2-15, 252);
    }else  if (iPhone6Plus_6sPlus) {
        return CGSizeMake(SCREEN_WIDTH/2-15, 272);
    }else{
        return CGSizeMake(SCREEN_WIDTH/2-15, 252);
    }
}
//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    // 1.定义重用标识
    static NSString *headerID= @"headerSelectType";

    
    GoodsModel *model=self.timeArr[indexPath.section][indexPath.row];
    
    static NSString *footerID = @"footerSelectType";
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID forIndexPath:indexPath];
    
    UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerID forIndexPath:indexPath];
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        headerView.backgroundColor=[UIColor groupTableViewBackgroundColor];
        for (UIView *view in headerView.subviews) {
            [view removeFromSuperview];
        }

        UIView *category=[[UIView alloc] init];
        [headerView addSubview:category];
        category.backgroundColor=[UIColor whiteColor];
        [category mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(headerView).with.offset(0);
            make.left.right.equalTo(headerView);
            make.height.mas_offset(44);
        }];
        
        UIImageView *img=[[UIImageView alloc] init];
        [headerView addSubview:img];
        img.backgroundColor=[UIColor orangeColor];
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(category).with.offset(7);
            make.bottom.equalTo(category).with.offset(-7);
            make.left.equalTo(category).with.offset(10);
            make.width.mas_offset(5);
        }];
        
        UILabel *categoryName=[[UILabel alloc] init];
        categoryName.text=model.register_date;
//        categoryName.text=@"8月31日";
        categoryName.font=[UIFont systemFontOfSize:17];
        categoryName.textColor=[UIColor colorWithHexString:@"#121212"];
        [headerView addSubview:categoryName];
        [categoryName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(img).with.offset(10);
            make.centerY.equalTo(img);
        }];
        headerView.backgroundColor=[UIColor redColor];
        return headerView;
    }
    if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
        for (UIView *view in headerView.subviews) {
            [view removeFromSuperview];
        }
        footerView.backgroundColor=[UIColor colorWithHexString:@"efeff4"];
        return footerView;
    }
    return nil;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsModel *model=self.goodsArr[indexPath.row];
    if(self.delageta && [self.delageta respondsToSelector:@selector(didSelectRowAtIndexPath:)]){
        [self.delageta didSelectRowAtIndexPath:model.uid];
    }
}
#pragma mark  DZNEmptyDataSetDelegate
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -22;
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    switch (self.emptyType) {
        case EmptyDataSetNone:
            return nil;
            break;
        case EmptyDataSetNoData:
            
            return [UIImage imageNamed:@"分类缺失"];
            break;
        case EmptyDataSetNoNetwork:
            
            return [UIImage imageNamed:@"断网"];
            break;
    }
}
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    
    switch (self.emptyType) {
        case EmptyDataSetNone:
            return NO;
            break;
        case EmptyDataSetNoData:
            return YES;
            break;
        case EmptyDataSetNoNetwork:
            return NO;
            break;
    }
}
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    if (self.emptyType==EmptyDataSetNone) {
        UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [activityView startAnimating];
        return activityView;
    }else{
        return nil;
    }
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    if (self.emptyType==EmptyDataSetNoData) {
        NSMutableAttributedString *currentPriceStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",Localized(@"当前分类暂无信息！"),Localized(@"先去其他地方逛逛吧。")]];
        [currentPriceStr addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Georgia-Bold" size:22],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#E6963F"]} range:NSMakeRange(0, [Localized(@"当前分类暂无信息！") length])];
        [currentPriceStr addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Georgia-Bold" size:18],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#C2AA93"]} range:NSMakeRange([Localized(@"当前分类暂无信息！") length], [Localized(@"先去其他地方逛逛吧。") length])];
        return currentPriceStr;
    }
    if (self.emptyType==EmptyDataSetNoNetwork){
        NSMutableAttributedString *currentPriceStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",Localized(@"您的网络不给力啊!"),Localized(@"请您检查是否联网。")]];
        [currentPriceStr addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Georgia-Bold" size:22],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#E6963F"]} range:NSMakeRange(0, [Localized(@"您的网络不给力啊!") length])];
        [currentPriceStr addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Georgia-Bold" size:18],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#C2AA93"]} range:NSMakeRange([Localized(@"您的网络不给力啊!") length], [Localized(@"请您检查是否联网。") length])];
        return currentPriceStr;
    }
    return nil;
}
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    if (self.emptyType==EmptyDataSetNoNetwork){
        NSMutableAttributedString *currentPriceStr = [[NSMutableAttributedString alloc] initWithString:Localized(@"重新加载")];
        [currentPriceStr addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Georgia-Bold" size:18],NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(0, [Localized(@"重新加载") length])];
        return currentPriceStr;
    }else{
        return nil;
    }
    
}

#pragma mark - DZNEmptyDataSetDelegate
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    //    [self loadDataWithGet_goods];
}

- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView {
    self.collectionView.contentOffset = CGPointZero;
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        //1.初始化layout
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        //设置collectionView滚动方向
        //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        //设置headerView的尺寸大小
//        layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 40);
        //该方法也可以设置itemSize
        layout.itemSize =CGSizeMake(110, 150);
        
        //2.初始化collectionView
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        //3.注册collectionViewCell
        //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
        [_collectionView registerClass:[GoodsListCell class] forCellWithReuseIdentifier:@"cell"];
        
        _collectionView.showsVerticalScrollIndicator=NO;
        //注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerSelectType"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerSelectType"];
        //4.设置代理
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.emptyDataSetSource=self;
        _collectionView.emptyDataSetDelegate=self;
    }
    return _collectionView;
}
//-(NSMutableArray *)goodsArr{
//    if (!_goodsArr) {
//        _goodsArr=[[NSMutableArray alloc] init];
//    }
//    return _goodsArr;
//}
-(NSMutableArray *)timeArr{
    if (!_timeArr) {
        _timeArr=[[NSMutableArray alloc] init];
    }
    return _timeArr;
}
-(void)reloadData{
    [self.collectionView reloadData];
}
@end
